//
//  UrlImageButton.m
//  test image
//
//  Created by Xuyan Yang on 8/06/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "UrlImageButton.h"
#import "SDWebImageManager.h"
#import "NSURLAdditions.h"
//#import "NSURLAdditions.h"
//#import "Macro.h"

@interface UrlImageButton()

-(NSURL*)getChangedURL:(NSURL*)url;
-(void)initArray;

@end

@implementation UrlImageButton

@synthesize iconIndex;
@synthesize picUrl;


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
		iconIndex = -1;
        frame_final=frame;
		isScale = NO;
	}
    return self;
}

- (void)dealloc {
    if (picUrl) {
        [picUrl release];
    }
	[super dealloc];
}

-(void)initArray
{
    if (!sizeArray) {
        sizeArray = [NSArray arrayWithObjects:@"ZoomImage",@"DetailImage",@"BigImage",@"NormalImage",@"ViewImage",@"SmallImage",@"ColorImage",@"ShowCaseImage",@"GridImage",@"LargeImage", nil];
    }
    
    if (!sizeDestArray) {
        sizeDestArray = [NSArray arrayWithObjects:@"ZoomImage", @"ZoomImage", @"DetailImage", @"LargeImage", @"BigImage", @"ViewImage", @"SmallImage", @"GridImage", @"LargeImage", @"ZoomImage", nil];
    }
}

- (void)setImage:(BOOL)animated withUrl:(NSString *)iconUrl withIsBkg:(BOOL)isBkg
{
    if(iconUrl == nil || [iconUrl isKindOfClass:[NSNull class]]){
        return;
    }
    
	_animated = animated;
	_isBackgroundImage = isBkg;
	picUrl = [[NSString alloc] initWithString:iconUrl];
	if(isBkg)
	{
		[self setBackgroundImage:[self getDefaultImage] forState:UIControlStateNormal];
	}
	else {
		[self setImage:[self getDefaultImage] forState:UIControlStateNormal];
	}

	NSURL* tempUrl = [NSURL URLWithString:iconUrl];
	
	NSURL* finallyUrl = nil;
	if([NSURL isWebURL:tempUrl])
	{
		finallyUrl = tempUrl;
	}
	else {
	}
	
	[self setImageWithURL:finallyUrl];
}	

- (void) setBackgroundImageFromUrl:(BOOL)animated withUrl:(NSString *)iconUrl
{       
	[self setImage:animated withUrl:iconUrl withIsBkg:YES];
}	

- (void) setImageFromUrl:(BOOL) animated withUrl:(NSString *)iconUrl
{       
     [self performSelector:@selector(loadImage:) withObject:iconUrl afterDelay:0.1];
	
}	
-(void)loadImage:(id)sender{
     NSString *icon=(NSString *)sender;
    [self setImage:YES withUrl:icon withIsBkg:NO];
}
	
- (void)setImageWithURL:(NSURL *)url
{
	[self setImageWithURL:url placeholderImage:[self getDefaultImage]];
}

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
{
	SDWebImageManager *manager = [SDWebImageManager sharedManager];
	
    // Remove in progress downloader from queue
    [manager cancelForDelegate:self];
	
    if(_isBackgroundImage)
	{
	    [self setBackgroundImage:placeholder forState:UIControlStateNormal];
	}
	else {
		[self setImage:placeholder forState:UIControlStateNormal];
	}
	
    if (url)
    {
        url = [self getChangedURL:url];
        [manager downloadWithURL:url delegate:self];
    }
    isLoad = NO;
}

- (void)cancelCurrentImageLoad
{
	[[SDWebImageManager sharedManager] cancelForDelegate:self];
}

- (BOOL)isLoad
{
    return isLoad;
}

-(UIImage*) getDefaultImage
{
	CGSize frameSize = self.frame.size;
    if(frameSize.width == 48 && frameSize.height == 48){
		return [UIImage  imageNamed:@"img_load_48x48.png"];
	}
	else if(frameSize.width == 73 && frameSize.height == 73){
		return [UIImage  imageNamed:@"img_load_73x73.png"];
	}
	else if(frameSize.width == 140 && frameSize.height == 140){
		return [UIImage  imageNamed:@"img_load_140x140.png"];
	}
	else if(frameSize.width == 160 && frameSize.height == 160){
		return [UIImage  imageNamed:@"img_load_160x160.png"];
	}
    else if(frameSize.width ==310 && frameSize.height == 310){
		return [UIImage  imageNamed:@"img_load_310x310.png"];
	}
	return nil;
}

-(NSURL*)getChangedURL:(NSURL*)url
{
    NSMutableString *strUrl = [NSMutableString stringWithString:url.absoluteString];
    if (!isRetina) {
        return url;
    }
    if (![strUrl hasPrefix:@"http://"]) {
        return url;
    }
    NSArray *array = [strUrl componentsSeparatedByString:@"/"];
    if ([array count] > 1) {
        NSString *strDest = [array objectAtIndex:[array count] - 2];
        if (!sizeArray) {
            [self initArray];
        }
        if ([sizeArray containsObject:strDest]) {
            NSRange range = [strUrl rangeOfString:strDest];
            int index = [sizeArray indexOfObject:strDest];
            [strUrl replaceCharactersInRange:range withString:[sizeDestArray objectAtIndex:index]];
        }
    }
    return (NSURL*)[NSURL URLWithString:strUrl];
}

#pragma mark -

- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image
{
//	if(_animated)
//	{
//        self.frame=CGRectMake(frame_final.origin.x+frame_final.size.width/2, frame_final.origin.y+frame_final.size.height/2, 0, 0);
//	    [UIView beginAnimations:nil context:nil];
//	    [UIView setAnimationDuration:0.5];
//        self.frame=frame_final;
//        [UIView commitAnimations];
//	}
	
	if(_isBackgroundImage)
	{
	    [self setBackgroundImage:image forState:UIControlStateNormal];
        [self setBackgroundImage:image forState:UIControlStateHighlighted];
	}
	else {
		[self setImage:image forState:UIControlStateNormal];
        [self setBackgroundImage:image forState:UIControlStateHighlighted];
	}
    isLoad = YES;
}

@end
